package com.dormitory_springboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dormitory_springboot.dto.StudentDTO;
import com.dormitory_springboot.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2022-06-01
 */
@Repository
@Mapper
public interface StudentMapper extends BaseMapper<Student> {

    @Select("SELECT s.id AS studentId, s.name AS studentName, d.id AS dormitoryId, d.name AS dormitoryName, " +
            "s.exercise_habit, s.boarding_experience, s.hygiene_habit, s.personality_traits " +
            "FROM student s " +
            "JOIN dormitory d ON s.dormitory_id = d.id " +
            "JOIN building b ON d.building_id = b.id " +
            "WHERE b.id = #{buildingId} " +
            "ORDER BY s.exercise_habit, s.boarding_experience, s.hygiene_habit, s.personality_traits;")
    List<StudentDTO> previewStudentsInBuilding(@Param("buildingId") String buildingId);


    @Update("UPDATE student SET dormitory_id = #{dormitoryId} WHERE id = #{studentId}")
    void assignStudentToDormitory(@Param("studentId") Integer studentId, @Param("dormitoryId") Integer dormitoryId);

}
